package com.wtwd.campus.fjryE15.controller;


import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.fjryE15.entity.FjryRespCode;
import com.wtwd.campus.fjryE15.entity.FjryResultVO;
import com.wtwd.campus.fjryE15.entity.PhoneDatas;
import com.wtwd.campus.fjryE15.entity.dto.ListDto;
import com.wtwd.campus.fjryE15.entity.dto.SetSpecialNumDto;
import com.wtwd.campus.fjryE15.entity.vo.AuthSetSpecialNum;
import com.wtwd.campus.fjryE15.entity.vo.FjRyVoData;
import com.wtwd.campus.fjryE15.entity.vo.ListVo;
import com.wtwd.campus.fjryE15.service.DeviceService;
import com.wtwd.campus.fjryE15.service.FjryAuthService;
import com.wtwd.campus.fjryE15.service.PhoneService;
import com.wtwd.campus.service.DeviceInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author mjy(此处用于福建睿翼e15扩展对接)
 * 睿翼->WT->设备
 * 1指令全量覆盖上一条覆盖下一条
 * 2.取消缓存重发
 * 3.将上一条指令记录置为失败
 * 4.覆盖号码数据，将号码数据置为待状态
 * 校验规则
 * 1.不在线不发
 * 2.是否超过号码最大组数
 * @date 2022/09/16
 * @description
 */
@RestController
@RequestMapping("/fjry/specialNumber")
public class PhoneController {

    private static final Logger logger = LoggerFactory.getLogger(PhoneController.class);

    @Resource
    private PhoneService phoneService;

    @Resource
    private DeviceInfoService deviceInfoService;

    @Resource
    private FjryAuthService fjryAuthService;

    @Resource
    private DeviceService deviceService;


    @PostMapping("/setSpecialNum")
    public FjryResultVO setSpecialNum(@RequestBody SetSpecialNumDto setSpecialNumDto) {
        logger.info("setSpecialNum setSpecialNumDto={}", setSpecialNumDto.toString());
        if (!fjryAuthService.authRequest(setSpecialNumDto.getTime(), setSpecialNumDto.getRandomStr(), setSpecialNumDto.getSign())) {
            return FjryResultVO.error(FjryRespCode.NO_RIGHT_OPERATION);
        }
        String imei = setSpecialNumDto.getImei();
        if (!deviceService.authDeviceIsExist(imei)){
            return FjryResultVO.error(FjryRespCode.DEVICE_NOT_EXIST);
        }
        AuthSetSpecialNum authSetSpecialNum = phoneService.AuthSetSpecialNum(imei,
                setSpecialNumDto.getSosLists(),
                setSpecialNumDto.getFamilyLists(), setSpecialNumDto.getWhiteLists());
        if (!authSetSpecialNum.isRest()) {
            return FjryResultVO.error(authSetSpecialNum.getRespCode());
        }
        try {
            DeviceInfo device = deviceInfoService.getDeviceByImei(setSpecialNumDto.getImei());
            phoneService.sendPhone(device.getDeviceId(), device.getImei(), setSpecialNumDto.getSosLists(), setSpecialNumDto.getFamilyLists(), setSpecialNumDto.getWhiteLists());
        } catch (Exception e) {
            e.printStackTrace();
            return FjryResultVO.error(FjryRespCode.FAILED);
        }
        return FjryResultVO.ok(FjRyVoData.createFjRyData());
    }

    @PostMapping("/list")
    public FjryResultVO list(@RequestBody ListDto listDto) {
        logger.info("list listDto={}", listDto.toString());
        if (!fjryAuthService.authRequest(listDto.getTime(), listDto.getRandomStr(), listDto.getSign())) {
            return FjryResultVO.error(FjryRespCode.NO_RIGHT_OPERATION);
        }
        if (!deviceService.authDeviceIsExist(listDto.getImei())){
            return FjryResultVO.error(FjryRespCode.DEVICE_NOT_EXIST);
        }
        DeviceInfo device = deviceInfoService.getDeviceByImei(listDto.getImei());
        PhoneDatas phoneDatas = phoneService.getPhonesByDeviceId(device.getDeviceId());
        return FjryResultVO.ok(ListVo.createListVo(phoneDatas.getSosList(), phoneDatas.getFamilyList(), phoneDatas.getWhiteList()));
    }


}
